package com.shy.collect.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shy.collect.entity.UserInfo;
import com.shy.collect.entity.UserPayConfig;
import com.shy.collect.service.UserService;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.converts.UserInfoConvert;
import com.shy.configs.mvc.models.AjaxResult;
import com.shy.configs.mvc.models.PageObject;
import com.shy.configs.mvc.models.query.UserEditQuery;
import com.shy.configs.mvc.models.vo.LikeUserInfoVo;
import com.shy.configs.mvc.models.vo.UserInfoVo;
import com.shy.configs.mvc.models.vo.UserPayConfigVo;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户操作
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {


    @Resource
    private UserService userService;

    @Resource
    private UserInfoConvert userInfoConvert;

    /**
     * 分页查询商户
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/pageList")
    public AjaxResult pageList(Integer pageNo, Integer pageSize) {
        pageNo = defPageNo(pageNo);
        pageSize = defPageSze(pageSize);

        //查询结果
        IPage<UserInfo> pageResult = userService.pageList(pageNo, pageSize);

        //pageResult能够获取分页信息和查询记录
        List<UserInfo> userList = pageResult.getRecords();
        // 分页对象
        PageObject pageObject = new PageObject(pageResult);

        List<UserInfoVo> userInfoVoList = userInfoConvert.convertListUserInfoVo(userList);
        // 指定返回数据
        return AjaxResult.success(wrapData(userInfoVoList, pageObject));
    }

    /**
     * 根据id查询商户
     */
    @GetMapping("/detail/{userNo}")
    public AjaxResult detail(@PathVariable String userNo) {
        if (StrUtil.isNotBlank(userNo)) {
            UserInfo userInfo = userService.getUserByUserNo(userNo);
            UserInfoVo userInfoVo = userInfoConvert.mapUserInfoVo(userInfo);
            return AjaxResult.success(userInfoVo);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }

    /**
     * 修改商户
     */
    @PostMapping("/edit")
    public AjaxResult userEdit(@RequestBody @Validated UserEditQuery editQuery) {
        userService.editUser(editQuery);
        return AjaxResult.success();
    }

    /**
     * 模糊查询商户名称列表
     */
    @GetMapping("/shopNameList")
    public AjaxResult shopNameList(String shopName) {
        if (StrUtil.isNotBlank(shopName) && shopName.length() > 1) {
            List<UserInfo> userInfoList = userService.likeShopNameList(shopName);
            //转换Vo
            List<LikeUserInfoVo> likeUserInfoVos = userInfoConvert.convertLikeUserInfoVoList(userInfoList);
            return AjaxResult.success(likeUserInfoVos);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }
}